// pages/order-list/order-list.js
var util = require("../../utils/util.js");
function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length === 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    navList: ['全部','待付款','待发货','待收货','待售后'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id==0){
      this.getOrderList()
    }else{
      this.getOrderList(options.id-1)
      this.setData({
          status: options.id
      });
    }
  },
  toDifferentStatus: function(e) {
      var status = e.currentTarget.dataset.status;
      this.setData({
          status
      });
      this.setData({
        order_list: []
      });
      if(status==0) {
        this.getOrderList()
      } else {
        this.getOrderList(status-1)
      }
  },
  toOrder: function(e) {
    wx.navigateTo({
        url: '/pages/order-detail/order-detail?id=' + e.currentTarget.dataset.id
    })
  },
  toService: function(e) {
    wx.navigateTo({
        url: '/pages/after-sale/after-sale?id=' + e.currentTarget.dataset.id
    })
  },
  toServiceDetail: function(e) {
    wx.navigateTo({
        url: '/pages/service-detail/service-detail?id=' + e.currentTarget.dataset.id
    })
  },
  toPresent: function (e) {
    wx.navigateTo({
      url: '/pages/present/present?id=' + e.currentTarget.dataset.id
    })
  },
  getOrderList: function (val='') {
    const _this = this
    util.request(getApp().api.goods.orderList,{
      openId: wx.getStorageSync('openid'),
      orderStatus: val,
      // orderType: 1
    }).then(
      function(res) {
        if (res.code == 200) {
          _this.setData({
            order_list: res.data
          });
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  confirm: function (e) {
    const _this = this
    util.request(getApp().api.goods.confrim,{
      orderId: e.currentTarget.dataset.id
    }).then(
      function(res) {
        if (res.code == 200) {
          _this.setData({
              status: 0
          });
          _this.getOrderList()
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  sendMsg: function () {
    this.setData({
      isShowFreight: !0
    });
  },
  closeFreight: function () {
    this.setData({
      isShowFreight: !1
    });
  },
  cancelOrder: function (e) {
    const _this = this
    util.request(getApp().api.goods.cancelOrder,{
      orderId: e.currentTarget.dataset.id
    }).then(
      function(res) {
        if (res.code == 200) {
          _this.setData({
              status: 0
          });
          _this.getOrderList()
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  toPay: function(e) {
    const _this = this
    util.request(getApp().api.wxpay.pay,{
      orderId: e.currentTarget.dataset.id
    }).then(
      function(res) {
        if (res.code == 200) {
          _this.setData({
              status: 0
          });
          _this.getOrderList()
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   	
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})